package Top101;

import java.util.ListIterator;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/3/28 12:09
 * BM1 反转链表
 */
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Solution1 {
    public ListNode ReverseList(ListNode head) {
        return reCall(null, head);
    }

    ListNode reCall(ListNode pre, ListNode cur) {
        if (cur == null) {
            return pre;
        }
        ListNode res = reCall(cur, cur.next);
        cur.next = pre;
        return res;

    }
    ///-----------------头插入
    public ListNode ReverseList1(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode cur = head;
        ListNode last = null ;
        while (cur != null){
            ListNode tem = cur.next;
            cur.next= last;
            last = cur;
            cur = tem;
        }
        return last;
    }
}
